
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cleverhans.attacks.fast_feature_adversaries &#8212; CleverHans  documentation</title>
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/alabaster.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cleverhans.attacks.fast_feature_adversaries</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">The FastFeatureAdversaries attack</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="c1"># pylint: disable=missing-docstring</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">tensorflow</span> <span class="k">as</span> <span class="nn">tf</span>

<span class="kn">from</span> <span class="nn">cleverhans.attacks.attack</span> <span class="kn">import</span> <span class="n">Attack</span>
<span class="kn">from</span> <span class="nn">cleverhans.compat</span> <span class="kn">import</span> <span class="n">reduce_sum</span>
<span class="kn">from</span> <span class="nn">cleverhans.model</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="kn">from</span> <span class="nn">cleverhans.utils_tf</span> <span class="kn">import</span> <span class="n">clip_eta</span>


<div class="viewcode-block" id="FastFeatureAdversaries"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.FastFeatureAdversaries">[docs]</a><span class="k">class</span> <span class="nc">FastFeatureAdversaries</span><span class="p">(</span><span class="n">Attack</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">  This is a fast implementation of &quot;Feature Adversaries&quot;, an attack</span>
<span class="sd">  against a target internal representation of a model.</span>
<span class="sd">  &quot;Feature adversaries&quot; were originally introduced in (Sabour et al. 2016),</span>
<span class="sd">  where the optimization was done using LBFGS.</span>
<span class="sd">  Paper link: https://arxiv.org/abs/1511.05122</span>

<span class="sd">  This implementation is similar to &quot;Basic Iterative Method&quot;</span>
<span class="sd">  (Kurakin et al. 2016) but applied to the internal representations.</span>

<span class="sd">  :param model: cleverhans.model.Model</span>
<span class="sd">  :param sess: optional tf.Session</span>
<span class="sd">  :param dtypestr: dtype of the data</span>
<span class="sd">  :param kwargs: passed through to super constructor</span>
<span class="sd">  &quot;&quot;&quot;</span>

  <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">sess</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">dtypestr</span><span class="o">=</span><span class="s1">&#39;float32&#39;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a FastFeatureAdversaries instance.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nb">super</span><span class="p">(</span><span class="n">FastFeatureAdversaries</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">dtypestr</span><span class="p">,</span>
                                                 <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">feedable_kwargs</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;eps&#39;</span><span class="p">,</span> <span class="s1">&#39;eps_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;clip_min&#39;</span><span class="p">,</span> <span class="s1">&#39;clip_max&#39;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">structural_kwargs</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;ord&#39;</span><span class="p">,</span> <span class="s1">&#39;nb_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;layer&#39;</span><span class="p">]</span>

    <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">Model</span><span class="p">)</span>

<div class="viewcode-block" id="FastFeatureAdversaries.parse_params"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.FastFeatureAdversaries.parse_params">[docs]</a>  <span class="k">def</span> <span class="nf">parse_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                   <span class="n">layer</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">eps</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span>
                   <span class="n">eps_iter</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span>
                   <span class="n">nb_iter</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
                   <span class="nb">ord</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span>
                   <span class="n">clip_min</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">clip_max</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Take in a dictionary of parameters and applies attack-specific checks</span>
<span class="sd">    before saving them as attributes.</span>

<span class="sd">    Attack-specific parameters:</span>

<span class="sd">    :param layer: (required str) name of the layer to target.</span>
<span class="sd">    :param eps: (optional float) maximum distortion of adversarial example</span>
<span class="sd">                compared to original input</span>
<span class="sd">    :param eps_iter: (optional float) step size for each attack iteration</span>
<span class="sd">    :param nb_iter: (optional int) Number of attack iterations.</span>
<span class="sd">    :param ord: (optional) Order of the norm (mimics Numpy).</span>
<span class="sd">                Possible values: np.inf, 1 or 2.</span>
<span class="sd">    :param clip_min: (optional float) Minimum input component value</span>
<span class="sd">    :param clip_max: (optional float) Maximum input component value</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Save attack-specific parameters</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">layer</span> <span class="o">=</span> <span class="n">layer</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">eps</span> <span class="o">=</span> <span class="n">eps</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">eps_iter</span> <span class="o">=</span> <span class="n">eps_iter</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">nb_iter</span> <span class="o">=</span> <span class="n">nb_iter</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">ord</span> <span class="o">=</span> <span class="nb">ord</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="o">=</span> <span class="n">clip_min</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="o">=</span> <span class="n">clip_max</span>

    <span class="c1"># Check if order of the norm is acceptable given current implementation</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]:</span>
      <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Norm order must be either np.inf, 1, or 2.&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
      <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;kwargs is unused and will be removed on or after &quot;</span>
                    <span class="s2">&quot;2019-04-26.&quot;</span><span class="p">)</span>

    <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="FastFeatureAdversaries.attack_single_step"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.FastFeatureAdversaries.attack_single_step">[docs]</a>  <span class="k">def</span> <span class="nf">attack_single_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">eta</span><span class="p">,</span> <span class="n">g_feat</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    TensorFlow implementation of the Fast Feature Gradient. This is a</span>
<span class="sd">    single step attack similar to Fast Gradient Method that attacks an</span>
<span class="sd">    internal representation.</span>

<span class="sd">    :param x: the input placeholder</span>
<span class="sd">    :param eta: A tensor the same shape as x that holds the perturbation.</span>
<span class="sd">    :param g_feat: model&#39;s internal tensor for guide</span>
<span class="sd">    :return: a tensor for the adversarial example</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">adv_x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">eta</span>
    <span class="n">a_feat</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">fprop</span><span class="p">(</span><span class="n">adv_x</span><span class="p">)[</span><span class="bp">self</span><span class="o">.</span><span class="n">layer</span><span class="p">]</span>

    <span class="c1"># feat.shape = (batch, c) or (batch, w, h, c)</span>
    <span class="n">axis</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">a_feat</span><span class="o">.</span><span class="n">shape</span><span class="p">)))</span>

    <span class="c1"># Compute loss</span>
    <span class="c1"># This is a targeted attack, hence the negative sign</span>
    <span class="n">loss</span> <span class="o">=</span> <span class="o">-</span><span class="n">reduce_sum</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">a_feat</span> <span class="o">-</span> <span class="n">g_feat</span><span class="p">),</span> <span class="n">axis</span><span class="p">)</span>

    <span class="c1"># Define gradient of loss wrt input</span>
    <span class="n">grad</span><span class="p">,</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">gradients</span><span class="p">(</span><span class="n">loss</span><span class="p">,</span> <span class="n">adv_x</span><span class="p">)</span>

    <span class="c1"># Multiply by constant epsilon</span>
    <span class="n">scaled_signed_grad</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps_iter</span> <span class="o">*</span> <span class="n">tf</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">grad</span><span class="p">)</span>

    <span class="c1"># Add perturbation to original example to obtain adversarial example</span>
    <span class="n">adv_x</span> <span class="o">=</span> <span class="n">adv_x</span> <span class="o">+</span> <span class="n">scaled_signed_grad</span>

    <span class="c1"># If clipping is needed,</span>
    <span class="c1"># reset all values outside of [clip_min, clip_max]</span>
    <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
      <span class="n">adv_x</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">clip_by_value</span><span class="p">(</span><span class="n">adv_x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">)</span>

    <span class="n">adv_x</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">stop_gradient</span><span class="p">(</span><span class="n">adv_x</span><span class="p">)</span>

    <span class="n">eta</span> <span class="o">=</span> <span class="n">adv_x</span> <span class="o">-</span> <span class="n">x</span>
    <span class="n">eta</span> <span class="o">=</span> <span class="n">clip_eta</span><span class="p">(</span><span class="n">eta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">eta</span></div>

<div class="viewcode-block" id="FastFeatureAdversaries.generate"><a class="viewcode-back" href="../../../source/attacks.html#cleverhans.attacks.FastFeatureAdversaries.generate">[docs]</a>  <span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">g</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generate symbolic graph for adversarial examples and return.</span>

<span class="sd">    :param x: The model&#39;s symbolic inputs.</span>
<span class="sd">    :param g: The target value of the symbolic representation</span>
<span class="sd">    :param kwargs: See `parse_params`</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Parse and save attack-specific parameters</span>
    <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">parse_params</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="n">g_feat</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">fprop</span><span class="p">(</span><span class="n">g</span><span class="p">)[</span><span class="bp">self</span><span class="o">.</span><span class="n">layer</span><span class="p">]</span>

    <span class="c1"># Initialize loop variables</span>
    <span class="n">eta</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">random_uniform</span><span class="p">(</span>
        <span class="n">tf</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">tf_dtype</span><span class="p">)</span>
    <span class="n">eta</span> <span class="o">=</span> <span class="n">clip_eta</span><span class="p">(</span><span class="n">eta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ord</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">cond</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">_</span><span class="p">):</span>
      <span class="k">return</span> <span class="n">tf</span><span class="o">.</span><span class="n">less</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nb_iter</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">body</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">e</span><span class="p">):</span>
      <span class="n">new_eta</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">attack_single_step</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">g_feat</span><span class="p">)</span>
      <span class="k">return</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">new_eta</span>

    <span class="n">_</span><span class="p">,</span> <span class="n">eta</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">while_loop</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span> <span class="n">body</span><span class="p">,</span> <span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">zeros</span><span class="p">([]),</span> <span class="n">eta</span><span class="p">),</span> <span class="n">back_prop</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                           <span class="n">maximum_iterations</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">nb_iter</span><span class="p">)</span>

    <span class="c1"># Define adversarial example (and clip if necessary)</span>
    <span class="n">adv_x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">eta</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
      <span class="n">adv_x</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">clip_by_value</span><span class="p">(</span><span class="n">adv_x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_max</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">adv_x</span></div></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../index.html">CleverHans</a></h1>








<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../source/attacks.html"><cite>attacks</cite> module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../source/model.html"><cite>model</cite> module</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>


  </body>
</html>